package cn.edu.nwafu.core.service.impl;

import cn.edu.nwafu.core.mapper.AircraftInfoMapper;
import cn.edu.nwafu.core.mapper.TicketInfoMapper;
import cn.edu.nwafu.core.mapper.UserInfoMapper;
import cn.edu.nwafu.core.pojo.entity.AircraftInfo;
import cn.edu.nwafu.core.pojo.entity.TicketInfo;
import cn.edu.nwafu.core.pojo.entity.UserInfo;
import cn.edu.nwafu.core.pojo.query.TicketInfoQuery;
import cn.edu.nwafu.core.pojo.vo.TicketInfoVo;
import cn.edu.nwafu.core.service.TicketInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhanghao
 * @since 2021-10-30
 */
@Service
public class TicketInfoServiceImpl extends ServiceImpl<TicketInfoMapper, TicketInfo> implements TicketInfoService {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Autowired
    private AircraftInfoMapper aircraftInfoMapper;

    @Override
    public List<TicketInfoVo> listInfoVo() {

        List<TicketInfo> ticketInfos = baseMapper.selectList(null);

        List<TicketInfoVo> ticketInfoVos = new ArrayList<>();

        for (TicketInfo ticketInfo : ticketInfos) {
            Long aircraftId = ticketInfo.getAircraftId();
            Long userId = ticketInfo.getUserId();

            QueryWrapper<UserInfo> userInfoQueryWrapper = new QueryWrapper<>();
            userInfoQueryWrapper.eq("id", userId);
            UserInfo userInfo = userInfoMapper.selectOne(userInfoQueryWrapper);

            QueryWrapper<AircraftInfo> aircraftInfoQueryWrapper = new QueryWrapper<>();
            aircraftInfoQueryWrapper.eq("id", aircraftId);
            AircraftInfo aircraftInfo = aircraftInfoMapper.selectOne(aircraftInfoQueryWrapper);

            TicketInfoVo ticketInfoVo = new TicketInfoVo(aircraftInfo, userInfo, ticketInfo);

            ticketInfoVos.add(ticketInfoVo);
        }

        return ticketInfoVos;
    }

    @Override
    public List<TicketInfoVo> listTicketById(Long userId, TicketInfoQuery ticketInfoQuery) {

        LocalDateTime createTime = ticketInfoQuery.getCreateTime();
        String reachCity = ticketInfoQuery.getReachCity();
        String startCity = ticketInfoQuery.getStartCity();
        Date startTime = ticketInfoQuery.getStartTime();


        QueryWrapper<TicketInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("user_id", userId)
                .eq(createTime != null, "create_time", createTime);

        List<TicketInfo> ticketInfos = baseMapper.selectList(queryWrapper);
        if (ticketInfos == null) {
            return null;
        }

        List<TicketInfoVo> list = new ArrayList<>();

        for (TicketInfo ticketInfo : ticketInfos) {
            Long aircraftId = ticketInfo.getAircraftId();
            QueryWrapper<AircraftInfo> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1
                    .eq("id", aircraftId)
                    .eq(StringUtils.isNotBlank(reachCity) && !"undefined".equals(reachCity), "reach_city", reachCity)
                    .eq(StringUtils.isNotBlank(startCity) && !"undefined".equals(startCity), "start_city", startCity)
                    .eq(startTime != null, "start_time", startTime);

            AircraftInfo aircraftInfo = aircraftInfoMapper.selectOne(queryWrapper1);
            log.warn("hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh：" + aircraftInfo);
            TicketInfoVo ticketInfoVo = new TicketInfoVo(aircraftInfo, null, ticketInfo);
            list.add(ticketInfoVo);
        }

        return list;
    }
}
